package com.yzcity.cinema.web.rest;

import com.yzcity.cinema.RedisTestContainerExtension;
import com.yzcity.cinema.YzcitycinemaApp;
import com.yzcity.cinema.domain.BaseTypeValue;
import com.yzcity.cinema.repository.BaseTypeValueRepository;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link BaseTypeValueResource} REST controller.
 */
@SpringBootTest(classes = YzcitycinemaApp.class)
@ExtendWith({ RedisTestContainerExtension.class, MockitoExtension.class })
@AutoConfigureMockMvc
@WithMockUser
public class BaseTypeValueResourceIT {

    private static final String DEFAULT_VALUE = "AAAAAAAAAA";
    private static final String UPDATED_VALUE = "BBBBBBBBBB";

    private static final String DEFAULT_DESCRIPTION = "AAAAAAAAAA";
    private static final String UPDATED_DESCRIPTION = "BBBBBBBBBB";

    private static final Integer DEFAULT_SEQUENCE = 1;
    private static final Integer UPDATED_SEQUENCE = 2;

    private static final Long DEFAULT_COMPANY_ID = 1L;
    private static final Long UPDATED_COMPANY_ID = 2L;

    private static final String DEFAULT_CREATED_BY = "AAAAAAAAAA";
    private static final String UPDATED_CREATED_BY = "BBBBBBBBBB";

    private static final Instant DEFAULT_CREATED_TIME = Instant.ofEpochMilli(0L);
    private static final Instant UPDATED_CREATED_TIME = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    private static final String DEFAULT_LAST_MODIFIED_BY = "AAAAAAAAAA";
    private static final String UPDATED_LAST_MODIFIED_BY = "BBBBBBBBBB";

    private static final Instant DEFAULT_LAST_MODIFIED_TIME = Instant.ofEpochMilli(0L);
    private static final Instant UPDATED_LAST_MODIFIED_TIME = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    @Autowired
    private BaseTypeValueRepository baseTypeValueRepository;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restBaseTypeValueMockMvc;

    private BaseTypeValue baseTypeValue;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static BaseTypeValue createEntity(EntityManager em) {
        BaseTypeValue baseTypeValue = new BaseTypeValue()
            .value(DEFAULT_VALUE)
            .description(DEFAULT_DESCRIPTION)
            .sequence(DEFAULT_SEQUENCE)
            .companyId(DEFAULT_COMPANY_ID)
            .createdBy(DEFAULT_CREATED_BY)
            .createdTime(DEFAULT_CREATED_TIME)
            .lastModifiedBy(DEFAULT_LAST_MODIFIED_BY)
            .lastModifiedTime(DEFAULT_LAST_MODIFIED_TIME);
        return baseTypeValue;
    }
    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static BaseTypeValue createUpdatedEntity(EntityManager em) {
        BaseTypeValue baseTypeValue = new BaseTypeValue()
            .value(UPDATED_VALUE)
            .description(UPDATED_DESCRIPTION)
            .sequence(UPDATED_SEQUENCE)
            .companyId(UPDATED_COMPANY_ID)
            .createdBy(UPDATED_CREATED_BY)
            .createdTime(UPDATED_CREATED_TIME)
            .lastModifiedBy(UPDATED_LAST_MODIFIED_BY)
            .lastModifiedTime(UPDATED_LAST_MODIFIED_TIME);
        return baseTypeValue;
    }

    @BeforeEach
    public void initTest() {
        baseTypeValue = createEntity(em);
    }

    @Test
    @Transactional
    public void createBaseTypeValue() throws Exception {
        int databaseSizeBeforeCreate = baseTypeValueRepository.findAll().size();
        // Create the BaseTypeValue
        restBaseTypeValueMockMvc.perform(post("/api/base-type-values")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(baseTypeValue)))
            .andExpect(status().isCreated());

        // Validate the BaseTypeValue in the database
        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeCreate + 1);
        BaseTypeValue testBaseTypeValue = baseTypeValueList.get(baseTypeValueList.size() - 1);
        assertThat(testBaseTypeValue.getValue()).isEqualTo(DEFAULT_VALUE);
        assertThat(testBaseTypeValue.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
        assertThat(testBaseTypeValue.getSequence()).isEqualTo(DEFAULT_SEQUENCE);
        assertThat(testBaseTypeValue.getCompanyId()).isEqualTo(DEFAULT_COMPANY_ID);
        assertThat(testBaseTypeValue.getCreatedBy()).isEqualTo(DEFAULT_CREATED_BY);
        assertThat(testBaseTypeValue.getCreatedTime()).isEqualTo(DEFAULT_CREATED_TIME);
        assertThat(testBaseTypeValue.getLastModifiedBy()).isEqualTo(DEFAULT_LAST_MODIFIED_BY);
        assertThat(testBaseTypeValue.getLastModifiedTime()).isEqualTo(DEFAULT_LAST_MODIFIED_TIME);
    }

    @Test
    @Transactional
    public void createBaseTypeValueWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = baseTypeValueRepository.findAll().size();

        // Create the BaseTypeValue with an existing ID
        baseTypeValue.setId(1L);

        // An entity with an existing ID cannot be created, so this API call must fail
        restBaseTypeValueMockMvc.perform(post("/api/base-type-values")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(baseTypeValue)))
            .andExpect(status().isBadRequest());

        // Validate the BaseTypeValue in the database
        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void checkValueIsRequired() throws Exception {
        int databaseSizeBeforeTest = baseTypeValueRepository.findAll().size();
        // set the field null
        baseTypeValue.setValue(null);

        // Create the BaseTypeValue, which fails.


        restBaseTypeValueMockMvc.perform(post("/api/base-type-values")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(baseTypeValue)))
            .andExpect(status().isBadRequest());

        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkSequenceIsRequired() throws Exception {
        int databaseSizeBeforeTest = baseTypeValueRepository.findAll().size();
        // set the field null
        baseTypeValue.setSequence(null);

        // Create the BaseTypeValue, which fails.


        restBaseTypeValueMockMvc.perform(post("/api/base-type-values")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(baseTypeValue)))
            .andExpect(status().isBadRequest());

        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void getAllBaseTypeValues() throws Exception {
        // Initialize the database
        baseTypeValueRepository.saveAndFlush(baseTypeValue);

        // Get all the baseTypeValueList
        restBaseTypeValueMockMvc.perform(get("/api/base-type-values?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(baseTypeValue.getId().intValue())))
            .andExpect(jsonPath("$.[*].value").value(hasItem(DEFAULT_VALUE)))
            .andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION)))
            .andExpect(jsonPath("$.[*].sequence").value(hasItem(DEFAULT_SEQUENCE)))
            .andExpect(jsonPath("$.[*].companyId").value(hasItem(DEFAULT_COMPANY_ID.intValue())))
            .andExpect(jsonPath("$.[*].createdBy").value(hasItem(DEFAULT_CREATED_BY)))
            .andExpect(jsonPath("$.[*].createdTime").value(hasItem(DEFAULT_CREATED_TIME.toString())))
            .andExpect(jsonPath("$.[*].lastModifiedBy").value(hasItem(DEFAULT_LAST_MODIFIED_BY)))
            .andExpect(jsonPath("$.[*].lastModifiedTime").value(hasItem(DEFAULT_LAST_MODIFIED_TIME.toString())));
    }
    
    @Test
    @Transactional
    public void getBaseTypeValue() throws Exception {
        // Initialize the database
        baseTypeValueRepository.saveAndFlush(baseTypeValue);

        // Get the baseTypeValue
        restBaseTypeValueMockMvc.perform(get("/api/base-type-values/{id}", baseTypeValue.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(baseTypeValue.getId().intValue()))
            .andExpect(jsonPath("$.value").value(DEFAULT_VALUE))
            .andExpect(jsonPath("$.description").value(DEFAULT_DESCRIPTION))
            .andExpect(jsonPath("$.sequence").value(DEFAULT_SEQUENCE))
            .andExpect(jsonPath("$.companyId").value(DEFAULT_COMPANY_ID.intValue()))
            .andExpect(jsonPath("$.createdBy").value(DEFAULT_CREATED_BY))
            .andExpect(jsonPath("$.createdTime").value(DEFAULT_CREATED_TIME.toString()))
            .andExpect(jsonPath("$.lastModifiedBy").value(DEFAULT_LAST_MODIFIED_BY))
            .andExpect(jsonPath("$.lastModifiedTime").value(DEFAULT_LAST_MODIFIED_TIME.toString()));
    }
    @Test
    @Transactional
    public void getNonExistingBaseTypeValue() throws Exception {
        // Get the baseTypeValue
        restBaseTypeValueMockMvc.perform(get("/api/base-type-values/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateBaseTypeValue() throws Exception {
        // Initialize the database
        baseTypeValueRepository.saveAndFlush(baseTypeValue);

        int databaseSizeBeforeUpdate = baseTypeValueRepository.findAll().size();

        // Update the baseTypeValue
        BaseTypeValue updatedBaseTypeValue = baseTypeValueRepository.findById(baseTypeValue.getId()).get();
        // Disconnect from session so that the updates on updatedBaseTypeValue are not directly saved in db
        em.detach(updatedBaseTypeValue);
        updatedBaseTypeValue
            .value(UPDATED_VALUE)
            .description(UPDATED_DESCRIPTION)
            .sequence(UPDATED_SEQUENCE)
            .companyId(UPDATED_COMPANY_ID)
            .createdBy(UPDATED_CREATED_BY)
            .createdTime(UPDATED_CREATED_TIME)
            .lastModifiedBy(UPDATED_LAST_MODIFIED_BY)
            .lastModifiedTime(UPDATED_LAST_MODIFIED_TIME);

        restBaseTypeValueMockMvc.perform(put("/api/base-type-values")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(updatedBaseTypeValue)))
            .andExpect(status().isOk());

        // Validate the BaseTypeValue in the database
        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeUpdate);
        BaseTypeValue testBaseTypeValue = baseTypeValueList.get(baseTypeValueList.size() - 1);
        assertThat(testBaseTypeValue.getValue()).isEqualTo(UPDATED_VALUE);
        assertThat(testBaseTypeValue.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
        assertThat(testBaseTypeValue.getSequence()).isEqualTo(UPDATED_SEQUENCE);
        assertThat(testBaseTypeValue.getCompanyId()).isEqualTo(UPDATED_COMPANY_ID);
        assertThat(testBaseTypeValue.getCreatedBy()).isEqualTo(UPDATED_CREATED_BY);
        assertThat(testBaseTypeValue.getCreatedTime()).isEqualTo(UPDATED_CREATED_TIME);
        assertThat(testBaseTypeValue.getLastModifiedBy()).isEqualTo(UPDATED_LAST_MODIFIED_BY);
        assertThat(testBaseTypeValue.getLastModifiedTime()).isEqualTo(UPDATED_LAST_MODIFIED_TIME);
    }

    @Test
    @Transactional
    public void updateNonExistingBaseTypeValue() throws Exception {
        int databaseSizeBeforeUpdate = baseTypeValueRepository.findAll().size();

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restBaseTypeValueMockMvc.perform(put("/api/base-type-values")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(baseTypeValue)))
            .andExpect(status().isBadRequest());

        // Validate the BaseTypeValue in the database
        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteBaseTypeValue() throws Exception {
        // Initialize the database
        baseTypeValueRepository.saveAndFlush(baseTypeValue);

        int databaseSizeBeforeDelete = baseTypeValueRepository.findAll().size();

        // Delete the baseTypeValue
        restBaseTypeValueMockMvc.perform(delete("/api/base-type-values/{id}", baseTypeValue.getId())
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<BaseTypeValue> baseTypeValueList = baseTypeValueRepository.findAll();
        assertThat(baseTypeValueList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
